//guide_page.js
const app = getApp();

Page({
  data: {
    hospitalDept: null,
    navbarData: {
      showCapsule: false, //是否显示左上角图标
      // title: '贝生超级医生', //导航栏 中间的标题
    },
    navHeight: app.globalData.statusBarHeight * 2 + 86,
    disabled: true,//设置禁用
    checked: false,
    count: 0
  },
  check: function (e) {
    var that = this;
    this.setData({
      checked: !that.data.checked
    })
  },
  toConsent: function (e) {
    console.log('hospitalDept', this.data.hospitalDept)
    wx.navigateTo({
      // url: "/pages/guide/consent/consent?url=" + e.currentTarget.dataset.url
      url: "../zhiqingshu/zhiqingshu"
    })
  },
  toJoin: function (e) {
    //点击同意加入后触发的事件
    if (!this.data.checked ) {
      wx.showToast({
        title: '请阅读并同意知情同意书',
        icon: 'none'
      })
      return;
    }
    var deptId = e.currentTarget.dataset.deptid;
    wx.request({
      url: app.getUrl('/xcx/synDept'),//该路径的跳转在进入登录界面之前执行
      method: 'GET',
      header: wx.getStorageSync('cookie'),
      data: {
        deptId: deptId
      },
      success(res) {
        if (res.data.code == 0) {
          app.globalData.deptId = deptId
        }
      }
    })
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      wx.request({
        url: app.getUrl('/xcx/getPhoneNumber'), //这里就写上后台解析手机号的接口
        //这里的几个参数是获取授权后的加密数据，作为参数传递给后台就行了
        data: {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv
        },
        method: 'GET',
        header: wx.getStorageSync('cookie'),
        success(res) {
          app.globalData.userMobile = res.data.data.phone;
          //手机号和验证码都正确的情况下进入
          if (res.data.code == 0) {
            if (res.data.data) {
              app.globalData.account = res.data.data.data;
            }
            wx.navigateTo({
              url: '../queue/queue',
            })
          } else if (res.data.code == 200) {
            app.globalData.userList = res.data.data.data;
            wx.navigateTo({
              url: '../user_list/userList',
            })
          } else if (res.data.code == 201) {
            app.globalData.userList = res.data.data.data;
            wx.navigateTo({
              url: '../queue_list/queueList?ptUserId=' + res.data.data.data[0].id,
            })
          } else {
            wx.showToast({
              title: '获取手机号失败',
              icon: 'none',
              complete() {
                setTimeout(function () {
                  wx.redirectTo({
                    url: "../../login/login",
                  })
                }, 1000)
              }
            })
          }
        }
      })
    } else {
      wx.redirectTo({
        url: "../../login/login",
      })
    }
  },
  //onLoad操作是在点击同意加入之前触发的事件，
  onLoad(query) {
    var that = this;
    console.log(query)
    const deptId = decodeURIComponent(query.deptId);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    //setTimeOut是为了在扫描二维码后再获取相关数据
    setTimeout(function () {
      that.loadDept(deptId);
    }, 800)
  },
  loadDept: function (deptId) {

    var that = this;
    wx.request({
      url: app.getUrl('/xcx/queryHospDept'),
      method: 'GET',
      header: wx.getStorageSync('cookie'),
      data: {
        deptId: "jmszxyydept001"
      },
      success(res) {


        if (res.data.code == 0) {
          that.setData({
            hospitalDept: res.data.data,
            disabled: false,
          })
          wx.setStorageSync('hospitalInfo', JSON.stringify(res.data.data))
          app.globalData.hospitalInfo = res.data.data
          if (res.data.data.miniBackEndUrl && res.data.data.miniBackEndUrl != app.globalData.config.server) {
            app.globalData.config.server = res.data.data.miniBackEndUrl;
            // app.login();
            setTimeout(function () {
              wx.hideLoading();
            }, 500)
          } else {
            wx.hideLoading();
          }
          //判断用户是否登陆过，如果微信用户的accountId和deptId相同，则说明该微信用户已经登陆过，直接跳转到主页或患者选择页面
          console.log('app.globalData', app.globalData)
          console.log('that.data.hospitalDept', that.data.hospitalDept)
          if (app.globalData.deptId == that.data.hospitalDept.id && app.globalData.userMobile) {
            wx.showLoading({
              title: '检查登录状态',
              mask: true,
              duration: 1000
            })

            setTimeout(function () {
              wx.request({
                url: app.getUrl('/xcx/pt/getSysUsersInfo'),
                method: 'GET',
                header: wx.getStorageSync('cookie'),
                success(res) {
                  console.log('res', res)
                  if (res.data.code == 200) {
                    app.globalData.userList = res.data.data
                    wx.redirectTo({
                      url: '/pages/guide/queue_list/queueList?ptUserId=' + res.data.data[0].id,
                    })
                  } else if (res.data.code == 0) {
                    app.globalData.userList = res.data.data;
                    wx.redirectTo({
                      url: '/pages/guide/user_list/userList',
                    })
                    that.setData({
                      checked: true
                    })
                  } else if (res.data.code == 404) {
                    wx.redirectTo({
                      url: '/pages/guide/queue/queue',
                    })
                  }
                }
              })
            }, 800)
          }
        } else if (res.data.code == 401) {

          console.log('that1', that.data.count)
          if (that.data.count < 7) {
            setTimeout(function () {
              that.data.count++; // 先递增
              that.setData({
                count: that.data.count // 再赋值
              });
              app.login();
              that.loadDept(deptId);
            }, 800)

          } else {
            wx.showToast({
              title: '请重新扫码登录',
              icon: 'none',
              duration: 2000,
            })
            return
          }

        } else if (res.data.code == 404) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000,
          })
        }
      }
    })
  }
})
